// 内容相关hooks

import { useState, useCallback } from 'react';
import {
  createContent as apiCreateContent,
  updateContentText as apiUpdateContentText,
  updateCover as apiUpdateCover,
  updateVideo as apiUpdateVideo,
  updateImages as apiUpdateImages,
  deleteContent as apiDeleteContent,
  getContentMedia as apiGetContentMedia,
  getMyContents as apiGetMyContents,
  getPublicContents as apiGetPublicContents,
  getAllContents as apiGetAllContents,
  viewContent as apiViewContent,
  getOperationLogs as apiGetOperationLogs,
} from '../api';
import {
  CreateContentRequest,
  UpdateContentTextRequest,
  UpdateCoverRequest,
  UpdateVideoRequest,
  UpdateImagesRequest,
  ContentQueryParams,
  OperationLogQueryParams,
  Content,
  ContentDetail,
  ContentMedia,
  ContentListResponse,
  OperationLogListResponse,
} from '../types';
import { handleApiError } from '../utils/api';

export const useContent = () => {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // 创建内容
  const createContent = useCallback(async (data: CreateContentRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiCreateContent(data);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 更新内容文本
  const updateContentText = useCallback(async (contentId: number, data: UpdateContentTextRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiUpdateContentText(contentId, data);
      if (response.code === 200) {
        return true;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 更新封面
  const updateCover = useCallback(async (contentId: number, data: UpdateCoverRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiUpdateCover(contentId, data);
      if (response.code === 200) {
        return true;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 更新视频
  const updateVideo = useCallback(async (contentId: number, data: UpdateVideoRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiUpdateVideo(contentId, data);
      if (response.code === 200) {
        return true;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 批量更新图片
  const updateImages = useCallback(async (contentId: number, data: UpdateImagesRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiUpdateImages(contentId, data);
      if (response.code === 200) {
        return true;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 删除内容
  const deleteContent = useCallback(async (contentId: number) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiDeleteContent(contentId);
      if (response.code === 200) {
        return true;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 获取内容媒体文件
  const getContentMedia = useCallback(async (contentId: number) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiGetContentMedia(contentId);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 获取用户自己的内容列表
  const getMyContents = useCallback(async (params: ContentQueryParams = {}) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiGetMyContents(params);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 获取公开内容列表
  const getPublicContents = useCallback(async (params: ContentQueryParams = {}) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiGetPublicContents(params);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 获取所有内容列表（管理员）
  const getAllContents = useCallback(async (params: ContentQueryParams = {}) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiGetAllContents(params);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 浏览内容详情
  const viewContent = useCallback(async (contentId: number) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiViewContent(contentId);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 获取操作日志
  const getOperationLogs = useCallback(async (params: OperationLogQueryParams = {}) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiGetOperationLogs(params);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    loading,
    error,
    createContent,
    updateContentText,
    updateCover,
    updateVideo,
    updateImages,
    deleteContent,
    getContentMedia,
    getMyContents,
    getPublicContents,
    getAllContents,
    viewContent,
    getOperationLogs,
  };
}; 